#include "StdAfx.h"
#include "TraderUI.h"
#include <Commctrl.h>

namespace Wi_Stock
{
	namespace Trader
	{

		using namespace System::Text::RegularExpressions;
		using namespace System::Net;
		using namespace System::IO;
		using namespace System::Threading;
		using namespace System::Runtime::InteropServices;

#define SEND_MSG(hwnd, msg, wparam, lparam)	\
	SendMessageA(hwnd, msg, (WPARAM)wparam, (LPARAM)lparam)

#define SEND_MSGW(hwnd, msg, wparam, lparam)	\
	SendMessageW(hwnd, msg, (WPARAM)wparam, (LPARAM)lparam)

		//-----------------------------------------------------------------------------
		void TraderUI::Open()
		{
			//CaptureAllWnd();
			//CaptureLvlII();
			//CaptureMonitor();

			//SendMessageA (_lvl2, WM_COMMAND, ctrlId, 0);
			//PostMessage (_lvl2, WM_COMMAND, (BN_CLICKED)<<8 | ctrlId, theBtn);
			//PostMessage (theBtn, BM_CLICK, 0, 0);
			//PostDlgItemMessageA(_lvl2, ctrlId, BM_CLICK, 0, 0); //OK!
		}

		//-----------------------------------------------------------------------------
		bool TraderUI::Send( SendInfo^ si )
		{

			return true;
		}

		//-----------------------------------------------------------------------------
		namespace CallBack
		{
			ref struct EnumWnd
			{
				static TraderUI^ _trdr;
				delegate BOOL FuncPtr(HWND, LPARAM);
				static GCHandle GCH;
				static BOOL Func(HWND   hwnd, LPARAM lparam)   
				{
					char className[255];
					GetWindowTextA(hwnd, className, 255);
					if(strlen(className))
					{
						char* pos =  strstr(className, "- Level II");
						if(pos){
							_trdr->_lvl2 = hwnd;
							return FALSE;
						}
					}
					return TRUE;
				}
			};
		};//namespace

		//-----------------------------------------------------------------------------
		void TraderUI::GetAllItem( HWND Group2, int ctrlId )
		{
			HRESULT hr;
			HWND cb = GetDlgItem(Group2, ctrlId);
			int count = SEND_MSG(cb, CB_GETCOUNT, 0, 0);

			wchar_t text[50];
			StringBuilder^ AllItem = gcnew StringBuilder;
			for(int i = 0; i<count; i++)
			{
				hr =SEND_MSGW(cb, CB_GETLBTEXT, i, text);	

				String^ strText = gcnew String(text);
				AllItem->Append("\"");
				AllItem->Append(strText);
				AllItem->Append("\"; ");
			}
			String^ all = AllItem->ToString();
			all;
		}

		//-----------------------------------------------------------------------------
		void TraderUI::CaptureMonitor()
		{
			const int bufSize = 32;

			//if(!_formPnS){
			//	_formPnS=FindWindowA(0, "AT001PL - Position Summary");
			//	if(!_formPnS) return;
			//}

			//EnumChildWindows(_formPnS, CallBack::Func, 0);
			//HWND group = FindWindowExA(_formPnS, 0, "#32770 (Dialog)", 0); //0xE900
			HWND sysListView = (HWND)0x000E0E84;// { unused=525576 }FindWindowExA(group, 0, "SysListView32", 0);//GetDlgItem(sysListView, 0x3F6); //0xE900

			char className[255];
			GetWindowTextA(sysListView, className, 255);

			DWORD pId;
			GetWindowThreadProcessId(sysListView, &pId);
			HANDLE farProc=OpenProcess(PROCESS_ALL_ACCESS, false, pId);
			if (!farProc)
				return;

			//farProc = GetCurrentProcess();

			HMODULE hm = GetModuleHandleA("Kernel32");
			PTHREAD_START_ROUTINE pfnThrdRtn = (PTHREAD_START_ROUTINE)GetProcAddress(hm, "LoadLibraryA");

			const char* dllPath = "F:\\game-logic-layer\\Stock\\OK\\LVInject.dll";
			int dllPathLen = strlen(dllPath) + 1;

			char* pFarBuf = (char*)VirtualAllocEx(farProc, NULL, dllPathLen, MEM_COMMIT, PAGE_READWRITE);
			BOOL ok = WriteProcessMemory(farProc, pFarBuf, dllPath, dllPathLen, NULL);

			HANDLE farThrd = CreateRemoteThread(farProc, 0, 0, pfnThrdRtn, pFarBuf, 0, 0);


			//int count=(int)SEND_MSG(sysListView, LVM_GETITEMCOUNT, 0, 0);
			//if(count == 0) 
			//	return;

			//LVITEM* pFarLVItm = (LVITEM*)VirtualAllocEx(farProc, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
			//char* pFarBuf = (char*)VirtualAllocEx(farProc, NULL, bufSize, MEM_COMMIT, PAGE_READWRITE);
			//if ((!pFarLVItm)||(!pFarBuf))
			//	return;

			//LVITEMA lvItem;
			//ZeroMemory(&lvItem, sizeof(LVITEMA));

			//lvItem.cchTextMax=bufSize;
			//lvItem.pszText=pFarBuf;
			////lvItem.mask = LVIF_TEXT;
			//lvItem.iSubItem = 0;
			////lvItem.iImage = 0;

			//char itmBuf[bufSize];

			//for(int i = 0; i < count; i++)
			//{
			//	lvItem.iItem = i;
			//	BOOL ok = WriteProcessMemory(farProc, pFarLVItm, &lvItem, sizeof(LVITEM), NULL);
			//	HRESULT res = SEND_MSG(sysListView, LVM_GETITEMTEXTA, i, pFarLVItm);
			//	ReadProcessMemory(farProc, pFarLVItm, &lvItem, sizeof(LVITEM), NULL);
			//	ok = ReadProcessMemory(farProc, lvItem.pszText, itmBuf, bufSize, NULL);
			//}

			//VirtualFreeEx(farProc, pFarLVItm, 0, MEM_RELEASE);
			VirtualFreeEx(farProc, pFarBuf, 0, MEM_RELEASE);
			CloseHandle(farProc);
		}

		//-----------------------------------------------------------------------------
		void TraderUI::CaptureLvlII()
		{
			if(!_lvl2) return;

			HWND Group1 = GetDlgItem(_lvl2, 0x1f5);
			HWND Group2 = GetDlgItem(_lvl2, 0x1f6);

			GetAllItem(Group2, 0x48a);

			HWND stkCodeInputList = GetDlgItem(Group1, 0x3f0);
			HWND stkCodeInput = GetDlgItem(stkCodeInputList, 0x3e9);

			HWND stkIDInputList = GetDlgItem(Group2, 0x488);
			HWND stkIDInput = GetDlgItem(stkIDInputList, 0x3e9);

			HRESULT hr;

			hr = SEND_MSG(stkCodeInput, WM_SETFOCUS, stkCodeInputList , 0);
			hr = SEND_MSG(stkCodeInput, WM_SETTEXT, 0, "MSFT");
			hr = SEND_MSG(stkCodeInput, WM_KILLFOCUS, 0 , 0);

			hr = SEND_MSG(stkIDInput, WM_SETTEXT, 0, "AT001PL");
		}

		//-----------------------------------------------------------------------------
		void TraderUI::CaptureAllWnd()
		{
			using namespace CallBack;
			EnumWnd::_trdr = this;
			EnumWnd::FuncPtr ^ det = gcnew EnumWnd::FuncPtr(&EnumWnd::Func);
			EnumWnd::GCH = GCHandle::Alloc(det);
			IntPtr funcPtr = Marshal::GetFunctionPointerForDelegate(det);
			WNDENUMPROC wndProc = (WNDENUMPROC)funcPtr.ToPointer();

			EnumWindows(wndProc, 0);

			EnumWnd::GCH.Free();
		}

	}//namespace
}//namespace
